package com.note.back.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class SmsService {

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private StringRedisTemplate redisTemplate;
    // 生成随机验证码
    public String generateVerificationCode() {
        Random random = new Random();
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            code.append(random.nextInt(10));
        }
        return code.toString();
    }
    // 发送验证码邮件
    public void sendVerificationCode(String toEmail) {
        String verificationCode = generateVerificationCode();
        redisTemplate.opsForValue().set(toEmail, verificationCode, 5, TimeUnit.MINUTES);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("思维捕手 <3581774657@qq.com>");
        message.setTo(toEmail);
        message.setSubject("验证码邮件");
        message.setText("您的验证码是：" + verificationCode + "，请在规定时间内完成验证。");
        javaMailSender.send(message);
    }
}